/*
 * Filename:    IndicatorsCalculationServiceImpl.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014年10月11日
 */
package com.paic.mhis.hcpms.quota.calculationreport.calculationjob.biz.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.paic.mhis.hcpms.quota.calculationreport.calculationdataItem.dto.CalculationResultDTO;
import com.paic.mhis.hcpms.quota.calculationreport.calculationjob.biz.service.IindicatorsCalculationService;
import com.paic.mhis.hcpms.quota.calculationreport.calculationjob.biz.service.KpiJobCtrlService;
import com.paic.mhis.hcpms.quota.calculationreport.calculationjob.constants.CalculationJobConstants;
import com.paic.mhis.hcpms.quota.calculationreport.calculationjob.dto.HcpmsKpiJobCtrlDTO;
import com.paic.mhis.hcpms.quota.calculationreport.calculationjob.dto.KpiBatchDetailDTO;
import com.paic.mhis.hcpms.quota.calculationreport.calculationjob.dto.KpiBatchJobLogDTO;
import com.paic.mhis.hcpms.quota.calculationreport.calculationjob.dto.KpiBatchParamterDTO;
import com.paic.mhis.hcpms.quota.calculationreport.calculationjob.intergration.dao.KpiBatchDao;
import com.paic.mhis.hcpms.quota.calculationreport.calculationkpi.biz.service.CalculationKpiService;
import com.pingan.um.client.util.UUID;

/**
 * 指标计算Job业务实现类
 * 
 * @author DUQIFANG262
 * 
 */
@Component("indicatorsCalculationServiceImpl")
public class IndicatorsCalculationServiceImpl implements IindicatorsCalculationService {

	public static Log log = LogFactory.getLog(IndicatorsCalculationServiceImpl.class);

	@Autowired
	private KpiBatchDao kpiBatchDao;
	@Autowired
	private CalculationKpiService calculationKpiService;
	@Autowired
	private KpiJobCtrlService kpiJobCtrlService;

	@Override
	public void processIndicatorsCalculation() {

		log.info("-----指标计算job进入-----");
		HcpmsKpiJobCtrlDTO ctrlDto = new HcpmsKpiJobCtrlDTO();
		ctrlDto.setJobCode("J20141217003");
		ctrlDto.setJobName("指标计算JOB");
		ctrlDto.setJobStatus(CalculationJobConstants.CTRL_JOB_STATUS_USE);
		ctrlDto.setTmpId(UUID.getID());
		ctrlDto = kpiJobCtrlService.getHcpmsJobCtrl(ctrlDto);
		// if (ctrlDto != null) {
		// ctrlDto.setTmpId(UUID.getID());
		// ctrlDto.setJobStatus(CalculationJobConstants.CTRL_JOB_STATUS_USE);
		// kpiJobCtrlService.updateHcpmsJobCtrl(ctrlDto);
		// }
		if (ctrlDto == null) {
			log.info("-----没有可计算的数据或者JOB被占用-----");
			return;
		}
		// 查询所有的数据项汇总完成的记录

		// String uuid = UUID.getID();
		//
		// this.kpiBatchDao.updateKpiBatchDetailDTOTempBatchByStatus(uuid,
		// CalculationJobConstants.KPI_BATCH_DETAIL_02);

		KpiBatchParamterDTO paramter = new KpiBatchParamterDTO();
		paramter.setStatus(CalculationJobConstants.KPI_BATCH_DETAIL_02);
		// paramter.setTempBatch(uuid);

		List<KpiBatchDetailDTO> list = this.kpiBatchDao.queryKpiBatchDetailDTO(paramter);

		if (null != list && list.size() > 0) {
			for (KpiBatchDetailDTO bean : list) {
				String id = bean.getIdKpiBatchDetail();
				String areCode = bean.getRegionCode();
				String month = bean.getMonth();
				String batchNo = bean.getBatchNo();
				// 更新当前记录为指标计算中
				this.kpiBatchDao.updateKpiBatchDetailDTOStatus(id, CalculationJobConstants.KPI_BATCH_DETAIL_03);

				// 处理指标计算
				log.info("-------指标计算开始-------");
				KpiBatchJobLogDTO logDto = new KpiBatchJobLogDTO();
				logDto.setBatchNo(batchNo);
				logDto.setCreateBy(bean.getCreatedBy());
				logDto.setUpdateBy(bean.getUpdatedBy());
				logDto.setCreateDate(new Date());
				logDto.setJobName(ctrlDto.getJobName());
				logDto.setJobNameDesc(KpiDataItemServiceImpl.class.getName());
				CalculationResultDTO result = calculationKpiService.calculationKpi(areCode, month);
				logDto.setUpdateDate(new Date());
				logDto.setOpertionTime(new Date());
				logDto.setSuccessMessage(result.getMessage(), areCode, month);
				if (CalculationJobConstants.status_N.equals(result.getIsError())) {
					logDto.setStatus(CalculationJobConstants.status_01);
				} else {
					logDto.setStatus(CalculationJobConstants.status_02);
				}
				kpiJobCtrlService.insertKpiJobLog(logDto);
				log.info("-------指标计算结束-------");

				// 更新当前记录为指标计算完成

				this.kpiBatchDao.updateBatchDetailTempBatchIsNull(CalculationJobConstants.KPI_BATCH_DETAIL_04, id);
			}
		} else {

			log.info("-----没有要计算的指标数据-----");
		}
		ctrlDto.setJobStatus(CalculationJobConstants.CTRL_JOB_STATUS_IDLE);
		ctrlDto.setTmpId(null);
		kpiJobCtrlService.updateHcpmsJobCtrl(ctrlDto);
	}
}
